home *** CD-ROM | disk | FTP | other *** search
/ Turnbull China Bikeride / Turnbull China Bikeride - Disc 2.iso / STUTTGART / LANG / C / LIB / UNIXLIB37B / !UnixLib37 / src / unix / c / pipe < prev    next >
Text File  |  1996-11-09  |  2KB  |  132 lines

  1. /****************************************************************************
  2.  *
  3.  * $Source: /unixb/home/unixlib/source/unixlib37/src/unix/c/RCS/pipe,v $
  4.  * $Date: 1996/10/30 22:04:51 $
  5.  * $Revision: 1.3 $
  6.  * $State: Rel $
  7.  * $Author: unixlib $
  8.  *
  9.  * $Log: pipe,v $
  10.  * Revision 1.3  1996/10/30 22:04:51  unixlib
  11.  * Massive changes made by Nick Burret and Peter Burwood.
  12.  *
  13.  * Revision 1.2  1996/05/06 09:01:35  unixlib
  14.  * Updates to sources made by Nick Burrett, Peter Burwood and Simon Callan.
  15.  * Saved for 3.7a release.
  16.  *
  17.  * Revision 1.1  1996/04/19 21:35:27  simon
  18.  * Initial revision
  19.  *
  20.  ***************************************************************************/
  21.  
  22. static const char rcs_id[] = "$Id: pipe,v 1.3 1996/10/30 22:04:51 unixlib Rel $";
  23.  
  24. #include <stdlib.h>
  25. #include <errno.h>
  26. #include <fcntl.h>
  27. #include <unistd.h>
  28.  
  29. #include <sys/os.h>
  30. #include <sys/dev.h>
  31. #include <sys/swis.h>
  32. #include <sys/unix.h>
  33. #include <sys/syslib.h>
  34.  
  35. int
  36. pipe (int *p)
  37. {
  38.   struct pipe *pi;
  39.   struct file *f0, *f1;
  40.   int fd0, fd1;
  41.   char file[32];
  42.   int pcnt;
  43.  
  44.   if ((fd0 = __fdalloc ()) < 0)
  45.     return (-1);
  46.   f0 = __u->file + fd0;
  47.   f0->dup = f0;
  48.   if ((fd1 = __fdalloc ()) < 0)
  49.     {
  50.       f0->dup = 0;
  51.       return (-1);
  52.     }
  53.   f1 = __u->file + fd1;
  54.   f0->dup = f1;
  55.   f1->dup = f0;
  56.  
  57.   {
  58.     register char *s = file;
  59.     register char *s2 = "/pipe/";
  60.     register int i;
  61.     char n[11];
  62.  
  63.     while (*s++ = *s2++);
  64.     s--;
  65.     pcnt = i = __intenv ("UnixLib$pcnt", 0);
  66.     s2 = n + 10;
  67.     *s2-- = 0;
  68.     do
  69.       {
  70.     *s2-- = (i % 10) + '0';
  71.     i /= 10;
  72.       }
  73.     while (i);
  74.     s2++;
  75.     while (*s++ = *s2++);
  76.   }
  77.  
  78.   f0->oflag = O_RDWR | O_CREAT | O_TRUNC | O_PIPE;
  79.  
  80.   {
  81.     int i;
  82.  
  83.     if ((i = __funcall ((*(__dev[DEV_PIPE].open)), (file, 0777, f0))) < 0)
  84.       {
  85.     f0->dup = f1->dup = 0;
  86.     return (-1);
  87.       }
  88.     f0->dev = f1->dev = makedev (DEV_PIPE, i);
  89.   }
  90.  
  91.   {
  92.     int r[10];
  93.     int v[1];
  94.  
  95.     v[0] = pcnt + 1;
  96.     r[0] = (int) "UnixLib$pcnt";
  97.     r[1] = (int) v;
  98.     r[2] = 4;
  99.     r[3] = 0;
  100.     r[4] = 1;
  101.     os_swi (OS_SetVarVal, r);
  102.   }
  103.  
  104.   f0->oflag = O_RDONLY | O_PIPE;
  105.   f1->oflag = O_WRONLY | O_PIPE;
  106.  
  107.   f0->pid = f1->pid = __u->pid;
  108.  
  109.   if (!(pi = malloc (sizeof (struct pipe))))
  110.     {
  111.       close (fd0);
  112.       close (fd1);
  113.       return (-1);
  114.     }
  115.   pi->p[0] = f0;
  116.   pi->p[1] = f1;
  117.   if (!(pi->file = __permstr (file)))
  118.     {
  119.       close (fd0);
  120.       close (fd1);
  121.       free (pi);
  122.       return (-1);
  123.     }
  124.   pi->next = __pipe;
  125.   __pipe = pi;
  126.  
  127.   p[0] = fd0;
  128.   p[1] = fd1;
  129.  
  130.   return (0);
  131. }
  132.